﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormPaymentList1 : FormTemplet
    {
        private PaymentCondition _paymentCondition = null;

        private DataTable _dtPayments = null;

        public FormPaymentList1()
        {
            this.InitializeComponent();
        }

        public FormPaymentList1(PaymentCondition pc) : this()
        {
            this._paymentCondition = pc;
            this.LoadPaymentsByCondition();
        }

        private void LoadPaymentsByCondition()
        {
            Payments payments = new Payments();
            try
            {
                this._dtPayments = payments.GetPaymentsByCondition(this._paymentCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            payments.Dispose();
            if (this._dtPayments != null)
            {
                if (this._dtPayments.Rows.Count < 1)
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgPayment.DataSource = null;
                }
                else
                {
                    this.dgPayment.DataSource = null;
                    this.dgPayment.DataSource = this._dtPayments;
                }
            }
        }

        private void FormPaymentInfo_Load(object sender, EventArgs e)
        {
            this.dgPayment.AutoResizeColumns();
        }

        private void btnShowTotal_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgPayment.Rows.Count; i++)
            {
                if (this.dgPayment.Rows[i].Cells["结算状态"].Value.ToString() == "已结算" || this.dgPayment.Rows[i].Cells["结算状态"].Value.ToString() == "被结算")
                {
                    this.dgPayment.Rows[i].Cells["结算状态"].Style.BackColor = Color.LawnGreen;
                }
                if (this.dgPayment.Rows[i].Cells["结算状态"].Value.ToString() == "作废")
                {
                    this.dgPayment.Rows[i].Cells["结算状态"].Style.BackColor = Color.Red;
                }
            }
            if (this._dtPayments != null)
            {
                if (this._dtPayments.Rows.Count < 1)
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    double num = 0.0;
                    for (int i = 0; i < this._dtPayments.Rows.Count; i++)
                    {
                        num += Convert.ToDouble(this._dtPayments.Rows[i][6]);
                    }
                    this.txtResult.Text = "总款项：" + num.ToString();
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgPayment.Rows[0].Cells[0].Value.ToString();
            }
            catch
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgPayment.CurrentCell.RowIndex;
            FormUpdatePayment1 formUpdatePayment = new FormUpdatePayment1((long)Convert.ToInt32(this.dgPayment.Rows[rowIndex].Cells[0].Value));
            formUpdatePayment.MdiParent = base.MdiParent;
            formUpdatePayment.Show();
            formUpdatePayment.Activate();
        }

        private void dgPayment_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex <= this.dgPayment.Rows.Count - 1)
            {
                new FormUpdatePayment(Convert.ToInt64(this.dgPayment.Rows[e.RowIndex].Cells[0].Value))
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
